﻿using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    public class FinishShippingService : BaseService<BaseEntity>, IFinishShippingService
    {
        private readonly IFinishShippingRepository _repository;
        public FinishShippingService(IFinishShippingRepository iRepository) : base(iRepository)
        {
            _repository = iRepository;
        }

        public List<WmsOutstock_Extend> GetWmsOutstocks(string orderno)
        {
            List<WmsOutstock_Extend> result=_repository.GetWmsOutstocks(orderno);
            return result;
        }

        public List<WmsOutstockDetail> GetWmsOutstockDetail(string orderno, int status)
        {
            List<WmsOutstockDetail> result=_repository.GetWmsOutstockDetail(orderno,status);
            return result;
        }

        public List<SysStock> GetStocks(string orderno,int status)
        {
            List<SysStock> stocks=_repository.GetSysStocks(orderno,status);    
            return stocks;
        }

        public List<SysStock> GetQtys(string cartonNo, int qty)
        {
            List<SysStock> stocks=_repository.GetQtys(cartonNo, qty);
            return stocks;
        }

        public string AddRackPackage(string cartonNo, int loginid, string shippingNo)
        {
            string result=_repository.AddRackPackage(cartonNo, loginid,shippingNo);
            return result;
        }

        public int UpdateStatusAndLocate(string cartonNo, string orderNo,int qty, int loginId)
        {
            int result=_repository.UpdateStatusAndLocate(cartonNo,orderNo,qty,loginId);
            return result;
        }

        public SetObjectDetail CheckMaterialSplit(string cartonNo, int splitNum, string loginId, int splitCount, string shippingNo)
        {

            SetObjectDetail det = new SetObjectDetail();
            //需要拆分的箱条码信息
            List<SysStock> MaterialInfo = _repository.GetMaterialInfo(cartonNo);
            if (MaterialInfo.Count == 0)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不存在，请检查!";
                return det;
            }
            if (MaterialInfo[0].Enabled.Equals("N"))
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:包装条码已被冻结，请检查!";
                return det;
            }
            if (MaterialInfo[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码不处于在库状态!";
                return det;
            }
            if (MaterialInfo[0].Qty < 1)
            {
                det.type = "NO";
                det.message = "[" + cartonNo + "]:箱条码数量不足，无法拆分!";
                return det;
            }


            List<SysStock> NewStock = _repository.CheckMaterialSplit(MaterialInfo, splitNum, loginId, splitCount,shippingNo);
            if (NewStock == null || NewStock.Count == 0)
            {
                det.type = "NO";
                det.message = "执行失败！请重试！";
            }
            else
            {
                det.type = "OK";
                det.wmsObjectDetail = NewStock.ConvertAll(s => (object)s);
            }
            return det;
        }

        

        public int UpdateOutStockDetail(string shippingNo, string cartono, int loginId)
        {
            int result=_repository.UpdateOutStockDetail(shippingNo, cartono, loginId);
            return result;
        }

        public int UpdateOutStock(string shippingNo, string loginId)
        {
            int result = _repository.UpdateOutStock(shippingNo, loginId);
            return result;
        }

        public int UpdateShippingLog(string shippingNo, string weight, string loginId)
        {
            int result = _repository.UpdateShippingLog(shippingNo, weight, loginId);
            return result;
        }

        public int GetDetailQty(string shippingNo, string cartonNo)
        {
            int result=_repository.GetDetailQty(shippingNo, cartonNo);
            return result;
        }

        public List<SysShippingLog> GetShippingLogs(string shippingNo)
        {
            List<SysShippingLog> result=_repository.GetShippingLogs(shippingNo);
            return result;
        }

        public List<WmsOutstockDetail> GetOutstockDetails(string shippingNo, string cartonNo)
        {
            List<WmsOutstockDetail> outstockDetails=_repository.GetOutstockDetails(shippingNo, cartonNo);
            return outstockDetails;
        }
    }
}
